package com.qfedu.service.impl;

import com.qfedu.mapper.ProjectMapper;
import com.qfedu.model.Project;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/27 12:46
 */

@Service
public class ProjectServiceImpl implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public List<Project> findAll() {
        return projectMapper.findAll();
    }

    @Override
    public PageResult findPage(QueryPageBean queryPage) {

        PageResult result = new PageResult();

        // 当前页码
        Integer currentPage = queryPage.getCurrentPage();
        // 每页记录数
        Integer pageSize = queryPage.getPageSize();

        List<Project> page = projectMapper.findPage((currentPage - 1) * pageSize, pageSize);

        Long count = projectMapper.count();

        result.setTotal(count);
        result.setRows(page);

        return result;
    }

    @Override
    @Transactional
    public int addProject(Project project) {
        project.setUpdatetime(new Date());
        int row = projectMapper.addProject(project);
        return row;
    }

    @Override
    public Project findByPid(int pid) {
        Project project = projectMapper.findProject(pid);
        return project;
    }

    @Override
    @Transactional
    public int edit(Project project) {
        project.setUpdatetime(new Date());
        int row = projectMapper.edit(project);
        return row;
    }

    @Override
    @Transactional
    public int deleteByPid(Integer pid) {
        int row = projectMapper.deleteByPid(pid);
        return row;
    }

    @Override
    @Transactional
    public void bindDid(String did, int[] pids) {
        if (pids != null && pids.length > 0) {
            for (int pid : pids) {
                projectMapper.bindDid(did,pid);
            }
        }
    }

    @Override
    @Transactional
    public void bindCid(String cid, int[] pids) {
        if (pids != null && pids.length > 0) {
            for (int pid : pids) {
                projectMapper.bindCid(cid,pid);
            }
        }
    }

    @Override
    public List<Project> findByDid(String did) {
        List<Project> list = projectMapper.findByDid(did);
        return list;
    }

    @Override
    @Transactional
    public void deleteDid(String did) {
        projectMapper.deleteDid(did);
    }

    @Override
    @Transactional
    public void deleteCid(String cid) {
        projectMapper.deleteDid(cid);
    }

}
